<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>register</title>
    <script src="js/jquery.js"></script>
</head>
<body>
<div>
    <div>
        邮箱: <input id="in_email" type="text" name="email"/><br/>
        密码: <input type="password" name="password"/><br/>
        确认密码: <input type="password" name="ConfirmPassword"/><br/>
        验证码: <input type="text" name="code"/><button id="create_code">获取验证码</button><br/>
        <button id="register_submit" type="submit" disabled="disabled">提交</button>
    </div>
</div>

<script>

    $("#in_email").blur(function () {
        email = $("#in_email").val();
        // 是否已注册
        $.ajax({
            type: "get",
            url: "/api/user/isRegistered/" + email,
            error: function () {
                alert("邮箱校验失败");
                $("#register_submit").removeAttribute("disabled")
            },
            success: function (data) {
                if (data === true) {
                    alert("该邮箱已注册");
                    $("#register_submit").attr("disabled", "disabled")
                } else {
                    $("#register_submit").removeAttr("disabled")
                }
            }
        })
    })

    $(document).on('click', '#register_submit', function () {
        email = $("input[name='email']").val();
        password = $("input[name='password']").val();
        ConfirmPassword = $("input[name='ConfirmPassword']").val();
        code = $("input[name='code']").val();
        if (email.length < 1) {
            alert("请先输入邮箱");
            return
        }

        if (password.length < 1) {
            alert("请先输入密码");
            return
        }
        if (password !== ConfirmPassword) {
            alert("密码不一致");
            return
        }
        if (code.length < 1) {
            alert("请输入验证码");
            return
        }
        $.ajax({
            type: "get",
            url: "/api/user/register/" + email + "/" + password + "/" + code,
            error: function () {
                alert("邮箱校验失败");
            },
            success: function (data) {
                if (data === true) {
                    window.location.href = "/welcome.html"
                } else {
                    alert("注册失败");
                }
            }

        })
    });

    var countdown = 60;

    function settime() {
        if (countdown == 0) {
            $('#create_code').html("获取验证码");
            countdown = 60;
            $("#create_code").removeAttr("disabled")
            return false;
        } else {
            $('#create_code').html("重新获取倒计时：" + countdown + "s");
            $("#create_code").attr("disabled", "disabled")
            countdown--;
        }
        setTimeout(function () {
            settime();
        }, 1000);
    }

    $(document).on('click', '#create_code', function () {
        email = $("input[name='email']").val()
        if (email.length < 1) {
            alert("请先输入邮箱");
            return
        }


           $.ajax({
            type: "get",
            url: "/api/code/create/" + email,
            error: function () {
                alert("获取验证码失败");
            },
            success: function (data) {
                if (data === true) {
                    alert("验证码已发送，请查收");
                    settime();
                } else {
                    alert("获取验证码失败");
                }
            }
        })

    });


</script>
</body>
</html>